package next.Functionality;

import next.TemplateActivity.NormalActivity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.os.Handler;
import android.os.Message;
import android.widget.Toast;

public class DialogFunctonality extends NormalActivity {

	    //--Variables--
		public static enum TypeDialog {ALERT,PROGRESS};
		protected ProgressDialog _progressDialog;
		protected Integer _progress;
		protected Handler _progressHandler;
	     
		//--Dialog Settings--
		protected String Title;
		protected Integer Icon;
		protected String  [] ButtonNames;
		protected CharSequence[] items;
		protected boolean[] itemsChecked;

		/** Called for a fast message, not persistent*/
		public void createToast(String message)
		{
			Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
		}
		
		/** Called before dialog is created, to set dialog features. */
		public void CreateDialog(TypeDialog type, Integer icon, String Title, String[] ButtonNames, String [] items, Integer cantidaBytes)
		{
			this.Title=Title;
			this.Icon=icon;
			this.ButtonNames=ButtonNames;
			this.items = items;
			this.itemsChecked = new boolean[items.length];
			this._progress=0;
			
			if (type==TypeDialog.ALERT){
				showDialog(0);
			}
			else if (type==TypeDialog.PROGRESS){
				
				//--Running a background thread--
				_progressHandler = new Handler() {
					public void handleMessage(Message msg) {
						super.handleMessage(msg);
						if (_progress >= 100) {
							_progressDialog.dismiss();
						} else {
							_progress++;
							//--_progressDialog.incrementProgressBy(1);--
							_progressDialog.setProgress(_progress);
							_progressHandler.sendEmptyMessageDelayed(0, 100);
						}
					}
				};
				
				showDialog(1);
				
				//--Progress variables reset--
				_progress = 0;
				_progressDialog.setProgress(0);
				_progressHandler.sendEmptyMessage(0);
			}


		}
		
		/** Called when the a dialog is created. */
		@Override
		public Dialog onCreateDialog(int id) {
			switch (id) {
			case 0:
				  return CreateAlertDialog();	
			case 1:
				  return CreateProgressDialog();
			default:	  
				  return null;
			}
		}

		/** Called when Alert dialog is created */
		public Dialog CreateAlertDialog()
		{
			return new AlertDialog.Builder(this)
			.setIcon(Icon)
			.setTitle(Title)
			.setPositiveButton(ButtonNames[0],
					new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog,
								int whichButton) {
								Toast.makeText(getBaseContext(),ButtonNames[0]+" clicked!",	Toast.LENGTH_SHORT).show();
						}
					})
			.setNegativeButton(ButtonNames[1],
					new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog,
								int whichButton) {
								Toast.makeText(getBaseContext(),ButtonNames[1]+" clicked!",	Toast.LENGTH_SHORT).show();
						}
					})
			.setMultiChoiceItems(items, itemsChecked,
					new DialogInterface.OnMultiChoiceClickListener() {

						public void onClick(DialogInterface dialog,
								int which, boolean isChecked) {
								Toast.makeText(getBaseContext(),items[which]+ (isChecked ? " checked!": " unchecked!"),
									Toast.LENGTH_SHORT).show();
						}
					}).create();
		}
		
		/** Called when Progress dialog is created */
		public Dialog CreateProgressDialog() {
		_progressDialog = new ProgressDialog(this);
		_progressDialog.setIcon(Icon);
		_progressDialog.setTitle(Title);
		_progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
		_progressDialog.setButton(DialogInterface.BUTTON_POSITIVE,
				ButtonNames[0], new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int whichButton) {
						Toast.makeText(getBaseContext(),
								ButtonNames[0] + " clicked!",
								Toast.LENGTH_SHORT).show();
					}
				});
		_progressDialog.setButton(DialogInterface.BUTTON_NEGATIVE,
				ButtonNames[1], new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int whichButton) {
						Toast.makeText(getBaseContext(),
								ButtonNames[1] + " clicked!",
								Toast.LENGTH_SHORT).show();
					}
				});
		return _progressDialog;

	}
	
	
	
}
